package com.caiyi.financial.nirvana.sms.subscribe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.caiyi.financial.nirvana.sms.cache.BlackMobileCache;
import com.caiyi.financial.nirvana.sms.cache.SmsMateCache;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPubSub;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ApiSubscribeListener extends JedisPubSub {

    private static final Logger logger = LoggerFactory.getLogger(ApiSubscribeListener.class);
    private static ApiSubscribeListener instance = new ApiSubscribeListener();

    private ApiSubscribeListener() {
    }

    public static ApiSubscribeListener getInstance() {
        return instance;
    }

    @Override
    public void onMessage(String channel, String message) {
        try {
            if (StringUtils.isBlank(message)) {
                logger.error("ApiSubscribeListener message is null !!! ");
                return;
            }

            Map map = JSON.parseObject(message, Map.class);
            String method = map.get("method") + "";
            logger.info("ApiSubscribeListener onMessage message {},method{}. ! ", message, method);
            Method fun = this.getClass().getDeclaredMethod(method, new Class[]{Map.class});
            fun.setAccessible(true);
            map.remove("method");
            fun.invoke(this, map);
        } catch (JSONException exception) {
            logger.info("ApiSubscribeListener onMessage JSONException {} ! ", message);
        } catch (Exception e) {
            logger.error("ApiSubscribeListener onMessage error ! ", e);
        }
    }


    private void addMobileList(Map map) throws Exception {
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            BlackMobileCache.addMobile(entry.getKey(), entry.getValue());
        }
        logger.info("添加账号黑名单集合到内存,调用方法[addSysListMobile],phone：{}.", map);
        return;
    }


    private void addMobile(Map map) throws Exception {
        String account = map.get("account") + "";
        String phone = map.get("phone") + "";
        BlackMobileCache.addMobile(account, phone);
        logger.info("添加账号黑名单到内存,调用方法[addMobile],account：{}，phone：{}.", account, phone);
        return;
    }

    private void delMobile(Map map) throws Exception {
        String account = map.get("account") + "";
        String phone = map.get("phone") + "";
        BlackMobileCache.delMobile(account, phone);
        logger.info("内存删除账号黑名单,调用方法[delMobile],account：{}，phone：{}.", account, phone);
        return;
    }

    private void addSysMobile(Map map) throws Exception {
        String phone = map.get("phone") + "";
        BlackMobileCache.addSysMobile(phone);
        logger.info("添加系统黑名单到内存,调用方法[addSysMobile],phone：{}.", phone);
        return;
    }

    private void delSysMobile(Map map) throws Exception {
        String phone = map.get("phone") + "";
        BlackMobileCache.delSysMobile(phone);
        logger.info("内存删除系统黑名单,调用方法[delSysMobile],phone：{}.", phone);
        return;
    }

    private void addSysListMobile(Map map) throws Exception {
        String[] phoneArr = JSON.parseObject(map.get("phones") + "", String[].class);
        if (null != phoneArr) {
            for (String phone : phoneArr) {
                BlackMobileCache.addSysMobile(phone);
            }
        }
        logger.info("添加系统黑名单集合到内存,调用方法[addSysListMobile],phone：{}.", phoneArr);
        return;
    }

    private void addSmsMateToLocal(Map map) throws Exception {
        SmsMateCache.addSmsMateToLocal(map);
        logger.info("加载sms_mate到内存,调用方法[addSmsMateToLocal]," + map);
        return;
    }

}
